import { Configuration, App } from '@midwayjs/core';
import * as koa from '@midwayjs/koa';
import * as validate from '@midwayjs/validate';
import * as info from '@midwayjs/info';
import { join } from 'path';
import { ReportMiddleware } from './middleware/report.middleware';
import * as swagger from '@midwayjs/swagger';
import * as sequelize from '@midwayjs/sequelize';
import * as jwt from '@midwayjs/jwt';
import {ValidateErrorFilter,UnauthorizedErrorFilter,ForbiddenErrorFilter,DefaultErrorFilter} from "./filter/validate.filter"
import { JwtMiddleware } from './middleware/jwt.middleware';
// import * as busboy from '@midwayjs/busboy';
import * as upload from '@midwayjs/upload';
import * as staticFile from '@midwayjs/static-file';
import { createPublic } from './util/createPublic';
import * as crossDomain from '@midwayjs/cross-domain';
@Configuration({
  imports: [
    koa,
    validate,
    {
      component: info,
      enabledEnvironment: ['local'],
    },
    sequelize,
    swagger,
    jwt,
    // busboy,
    upload,
    staticFile,
    crossDomain
  ],
  importConfigs: [join(__dirname, './config')],
})
export class MainConfiguration {
  @App('koa')
  app: koa.Application;

  async onReady() {
    // add middleware
    this.app.useMiddleware([ReportMiddleware,JwtMiddleware]);
    // add filter
    // this.app.useFilter([NotFoundFilter, DefaultErrorFilter]);
    this.app.useFilter([ValidateErrorFilter, UnauthorizedErrorFilter, ForbiddenErrorFilter,DefaultErrorFilter]);
    // this.app.useMiddleware(busboy.UploadMiddleware);
    createPublic()
  }
}
